package com.hand.todo.api.controller.v1;

import com.hand.todo.config.SoHeaderTag;
import com.hand.todo.domain.entity.HodrSoHeader;
import com.hand.todo.app.service.SoHeaderService;
import com.hand.todo.domain.entity.SoHeaderVo;
import com.hand.todo.infra.mapper.SoHeaderMapper;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ArrayUtils;
import org.hzero.core.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import sun.security.util.ArrayUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = SoHeaderTag.TAG)
@RestController("SoHeaderController.v1")
@RequestMapping("/hptl/v1")
public class SoHeaderController {
    @Autowired
    private SoHeaderService headerService;
    @Autowired
    private SoHeaderMapper headerMapper;

    @ApiOperation("订单查询总api")
    @Permission(level = ResourceLevel.SITE)
    @GetMapping("/{organizationId}/so-headers/dto")
    public ResponseEntity<Page<SoHeaderVo>> list(
            @PathVariable("organizationId") long orgId,
            @RequestParam(name = "companyName", required = false) Long companyName,
            @RequestParam(name = "customerName", required = false) Long customerName,
            @RequestParam(name = "orderNumber", required = false) String orderNumber,
            @RequestParam(name = "orderStatus", required = false) String orderStatus,
            @RequestParam(name = "itemCode", required = false) String itemCode,
            PageRequest page
    ) {
        HodrSoHeader soHeadervo = new HodrSoHeader();
        if (companyName != null) {
            soHeadervo.setCompanyId(companyName);
        }
        if (customerName != null) {
            soHeadervo.setCustomerId(customerName);
        }
        if (orderNumber != null) {
            soHeadervo.setOrderNumber(orderNumber);
        }
        if (orderStatus != null) {
            soHeadervo.setOrderStatus(orderStatus);
        }
        if (itemCode != null) {
            soHeadervo.setItemCode(itemCode);
        }
        System.out.println(soHeadervo.toString());
        return Results.success(headerService.queryHeaders(page, soHeadervo));
    }

    @ApiOperation("订单汇总删除")
    @Permission(level = ResourceLevel.SITE)
    @DeleteMapping("/{organizationId}/so-headers/batch")
    public void remove(
            @PathVariable("organizationId") long orgId,
            @RequestBody List<HodrSoHeader> list
    ) {
        Long[] ids = list.stream()
                .map(HodrSoHeader::getSoHeaderId)
                .collect(Collectors.toList())
                .toArray(ArrayUtils.EMPTY_LONG_OBJECT_ARRAY);
        headerService.deleteByList(Arrays.asList(ids));
    }


    @ApiOperation("保存订单头行结构")
    @Permission(level = ResourceLevel.SITE)
    @PostMapping("/{organizationId}/so-headers/save")
    public ResponseEntity<HodrSoHeader> save(
            @PathVariable("organizationId") long orgId,
            @RequestBody HodrSoHeader soHeader
    ) {
        // soHeader 中包括List<SoLine>
        return Results.success(headerService.save(soHeader));
    }

    @ApiOperation("更改订单头信息")
    @Permission(level = ResourceLevel.SITE)
    @PutMapping("/{organizationId}/so-headers/update")
    public ResponseEntity<HodrSoHeader> update(
            @PathVariable("organizationId") long orgId,
            @RequestBody HodrSoHeader soHeader
    ) {
        return Results.success(headerService.update(soHeader));
    }

    @ApiOperation("订单提交")
    @Permission(level = ResourceLevel.SITE)
    @PostMapping("/{organizationId}/so-headers/submit")
    public void submit(
            @PathVariable("organizationId") long orgId,
            @RequestBody HodrSoHeader soHeader
    ) {
        headerService.submit(orgId, soHeader);
    }

    @ApiOperation("根据订单头id获取订单信息")
    @Permission(level = ResourceLevel.SITE)
    @GetMapping("/{organizationId}/so-headers/soHeaderId/{headerId}")
    public ResponseEntity<HodrSoHeader> get(
            @PathVariable("organizationId") long organizationId,
            @PathVariable("headerId") long headerId
    ) {
        return Results.success(headerService.queryHeaderById(headerId ));
    }
}
